package com.twt.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twt.common.pojo.Result;
import com.twt.common.utils.Assert;
import com.twt.model.system.entity.SysAccount;
import com.twt.sys.service.SysAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/sys/account")
public class SysAccountController {

    @Autowired
    private SysAccountService sysAccountService;

    @PostMapping("/add")
    public Result add(SysAccount sysAccountAddVo) {
        sysAccountService.addAccount(sysAccountAddVo);
        return Result.ok();
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(String ids) {
        sysAccountService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok();
    }

    @PutMapping("/update")
    public Result update(SysAccount sysAccountEditVo) {
        Assert.isBlank(sysAccountEditVo.getId(), "编辑主键不能为空");
        sysAccountService.updateById(sysAccountEditVo);
        return Result.ok();
    }

    @GetMapping("/query/page/{currentPage}/{pageSize}")
    public Result queryPageSysDict(@PathVariable("currentPage") long currentPage, @PathVariable("pageSize") long pageSize, SysAccount queryVo) {
        Page<SysAccount> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        LambdaQueryWrapper<SysAccount> lambdaQueryWrapper = Wrappers.lambdaQuery(queryVo);

        Page<SysAccount> sysDictPage = sysAccountService.page(page, lambdaQueryWrapper);
        return Result.ok(sysDictPage);
    }

    @GetMapping("/query/one/{id}")
    public Result queryOneById(@PathVariable("id") String id) {
        SysAccount sysAccountInfoVo = sysAccountService.getById(id);
        return Result.ok(sysAccountInfoVo);
    }
}
